﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TPA.DataAccess;

namespace TVProgramAnalyst.Tools
{
    public  enum SystemCodeEditorMode { Insert, Edit }
    
    public partial class frmSystemCode : Form
    {
        SystemCodeEditorMode _editormode = SystemCodeEditorMode.Edit;
        //TreeNode _currentnode = null;

        public frmSystemCode()
        {
            InitializeComponent();
        }

        private void frmSystemCode_Load(object sender, EventArgs e)
        {
            LoadSystemCodeTree();
            tvSystemCode.ExpandAll();
            tvSystemCode.SelectedNode = tvSystemCode.Nodes[0];
            tvSystemCode.HideSelection = false;
        }

        private void LoadSystemCodeTree()
        {
            //BLLFactory.SystemCodeBLL.Refresh();
            List<SystemCode> root = BLLFactory.SystemCodeBLL.GetRoot(true);
            foreach (SystemCode code in root)
            {
                TreeNode node = tvSystemCode.Nodes.Add(code.Id.ToString(), code.Name);
                node.ToolTipText = code.Id.ToString();
                LoadSubCodeNode(code, node);
                //tvSystemCode.Nodes.Add(code.Id.ToString(), code.Name).ToolTipText=code.Id.ToString();
            }
        }

        /// <summary>
        /// Load sub code node (recursion)
        /// </summary>
        /// <param name="code">SystemCode</param>
        /// <param name="node">TreeNode</param>
        private void LoadSubCodeNode(SystemCode code, TreeNode node)
        {
            foreach (SystemCode c in code.SubSystemCodes)
            {
                TreeNode subnode = node.Nodes.Add(c.Id.ToString(), c.Name);
                subnode.ToolTipText = c.Id.ToString();
                if (c.SubSystemCodes.Count > 0)
                    LoadSubCodeNode(c, subnode);
            }
            
        }

        private void tvSystemCode_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //e.Node.Nodes.Clear();
            //LoadSubCodeNode(BLLFactory.SystemCodeBLL.Get(int.Parse(e.Node.ToolTipText)), e.Node);
        }

        private void tvSystemCode_MouseClick(object sender, MouseEventArgs e)
        {
            
        }

        //Load SystemCode Detail
        private void LoadSystemCode(int codeid)
        {
            SystemCode code = BLLFactory.SystemCodeBLL.Get(codeid);
            if (code != null)
            {
                lblId.Text = code.Id.ToString();
                txtDescription.Text = code.Description;
                txtName.Text = code.Name;
                txtEnglishDescription.Text = code.EnglishDescription;
                txtEnglishName.Text = code.EnglishName;
                txtSortOrder.Text = code.SortOrder.ToString();
                LoadCBLParent(code.Parent);
                chbEnabled.Checked = code.Enabled;
            }
        }

        private void LoadCBLParent(SystemCode parent)
        {
            cblParent.Items.Clear();
            if (parent != null)
            {
                cblParent.ValueMember = "Id";
                cblParent.DisplayMember = "Name";
                cblParent.Items.Add(parent);
                cblParent.SelectedIndex = 0;
            }
            else
            {
                parent = new SystemCode();
                parent.Id = -1;
                parent.Name = "Root";
                cblParent.ValueMember = "Id";
                cblParent.DisplayMember = "Name";
                cblParent.Items.Add(parent);
                cblParent.SelectedIndex = 0;
            }
        }

        //Load default value for new SystemCode with parentid
        private void LoadDefaultNewSystemCode(int parentid)
        {
            lblId.Text = "";
            txtDescription.Text = "";
            txtName.Text = "";
            txtEnglishDescription.Text = "";
            txtEnglishName.Text = "";
            txtSortOrder.Text = "";
            chbEnabled.Checked = false;
            LoadCBLParent(BLLFactory.SystemCodeBLL.Get(parentid));
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            _editormode = SystemCodeEditorMode.Edit;
            LoadSystemCode(int.Parse(tvSystemCode.SelectedNode.ToolTipText));            
        }

        private void tvSystemCode_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            tvSystemCode.SelectedNode = e.Node; 
            if (e.Button == MouseButtons.Right)
            {
                //control system code menagement level
                //if (e.Node.Level < 1)
                //{
                //    btnAddNew.Visible = false;
                //    btnDelete.Visible = false;
                //}
                //else
                //{
                //    btnAddNew.Visible = true;
                //    btnDelete.Visible = true;
                //}
                menuCodeTree.Show();
                //MessageBox("
            }
            else if (e.Button == MouseButtons.Left)
            {
                _editormode = SystemCodeEditorMode.Edit;
                //LoadSystemCode(int.Parse(e.Node.ToolTipText));
            }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            _editormode = SystemCodeEditorMode.Insert;
            LoadDefaultNewSystemCode(int.Parse(tvSystemCode.SelectedNode.ToolTipText));
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (tvSystemCode.SelectedNode.Nodes.Count == 0)
            {
                BLLFactory.SystemCodeBLL.Remove(int.Parse(tvSystemCode.SelectedNode.ToolTipText));
                BLLFactory.SystemCodeBLL.SaveChanges();
                tvSystemCode.SelectedNode.Remove();
                tvSystemCode.SelectedNode = tvSystemCode.SelectedNode.Parent;
            }
            else
            {
                MessageBox.Show("Please delete sub node first!");
            }
            //}
            //catch(Exception ex)
            //{
            //    MessageBox.Show("Please delete sub node first!\n\r\n\rError Details:\n\r"+ex.Message);
            //    //BLLFactory.SystemCodeBLL.Refresh();
            //}
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SystemCode code = new SystemCode();
            //if editormode==edit
            if (_editormode == SystemCodeEditorMode.Edit)
            {
                code = BLLFactory.SystemCodeBLL.Get(int.Parse(lblId.Text));
            }
            //if editormode==insert
            code.Name = txtName.Text;
            code.Description = txtDescription.Text;
            code.EnglishName = txtEnglishName.Text;
            code.EnglishDescription = txtEnglishDescription.Text;
            int sortorder=0;
            int.TryParse(txtSortOrder.Text, out sortorder);
            code.Enabled = chbEnabled.Checked;
            if (cblParent.SelectedItem != null)
                code.ParentId = ((SystemCode)cblParent.SelectedItem).Id;
            else
                code.ParentId = -1;
            if (_editormode == SystemCodeEditorMode.Insert)
            {
                BLLFactory.SystemCodeBLL.Add(code);
            }
            BLLFactory.SystemCodeBLL.SaveChanges();
            if (_editormode == SystemCodeEditorMode.Insert)
            {
                //add new node under selected node
                TreeNode addednode=tvSystemCode.SelectedNode.Nodes.Add(code.Id.ToString(), code.Name);
                addednode.ToolTipText = code.Id.ToString();
                tvSystemCode.SelectedNode = addednode;
            }
            else
            {
                //update the selected node text
                tvSystemCode.SelectedNode.Text = code.Name;
            }
        }

        private void tvSystemCode_AfterSelect(object sender, TreeViewEventArgs e)
        {
            _editormode = SystemCodeEditorMode.Edit;
            LoadSystemCode(int.Parse(e.Node.ToolTipText));
        }



    }
}
